﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using SortOrder = System.Windows.Forms.SortOrder;
using System.Web.Services;


namespace SecurityDev
{
    public partial class ObjectsForm : Form
    {
        public ObjectsForm()
        {
            InitializeComponent();
        }

        private void ObjectsForm_Load(object sender, EventArgs e)
        {
            objDataGridView.DataSource = DS.outpost._object;

            objDataGridView.Columns["obj_seq"].HeaderText = "Object ID";
            objDataGridView.Columns["obj_name"].HeaderText = "Name";
            objDataGridView.Columns["obj_phone"].HeaderText = "Phone";
            objDataGridView.Columns["obj_schema"].HeaderText = "schemas";
            objDataGridView.Columns["obj_path"].HeaderText = "Paths";
            objDataGridView.Columns["obj_photo_url"].HeaderText = "Photos URL";
            objDataGridView.Columns["obj_adress"].HeaderText = "Address";
            objDataGridView.Columns["obj_priority"].HeaderText = "Priority";
            objDataGridView.Columns["obj_category"].HeaderText = "Category";
            objDataGridView.Columns["obj_coord"].HeaderText = "Coordinates";
            objDataGridView.Columns["obj_region"].HeaderText = "Region";
        }

        private void DoneBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            this.Cursor = Cursors.AppStarting;

            decimal obj_seq = (decimal)objDataGridView.Rows[e.RowIndex].Cells[0].Value;
            var objR = DS.outpost._object.FindByobj_seq(Convert.ToDecimal(obj_seq));
            /*
            var person_seq = (from op in outpostDS.object_person
                              where op.obj_seq == objR.obj_seq
                           select op.person_seq).LastOrDefault();
            var personR = (from p in outpostDS.person
                           where p.person_seq == person_seq
                           select p).LastOrDefault();
            */
            ObjectDetail objectDetail = null;
            try
            {
                objectDetail = new ObjectDetail(obj_seq);
            }
            catch (NullReferenceException ex)
            {
                MessageBox.Show(ex.Message);
            }
            if (objectDetail != null)
            {
                objectDetail.ShowDialog();
            }
            this.Cursor = Cursors.Default;
        }

        private void AddObjBtn_Click(object sender, EventArgs e)
        {
            InitializeObject dialog = new InitializeObject();
            dialog.ShowDialog();
        }

        private void changeObjBtn_Click(object sender, EventArgs e)
        {
            InitializeObject dialog = new InitializeObject((decimal)objDataGridView.SelectedRows[0].Cells[0].Value);
            dialog.ShowDialog();
        }

        private void deleteBtn_Click(object sender, EventArgs e)
        {
            OutpostDS.objectRow r = DS.outpost._object.FindByobj_seq((decimal)objDataGridView.SelectedRows[0].Cells[0].Value);

            foreach (var obj_perR in r.GetChildRows("op_ibfk_2"))//obj<-ObjPer
            {
                obj_perR.Delete();
            }
            foreach (var dvrR in r.GetChildRows("dvr_ibfk_1"))//obj<-dvr
            {
                foreach (var alertR in dvrR.GetChildRows("alert_ibfk_1"))//dvr<-alert
                {
                    alertR.Delete();
                }
                dvrR.Delete();
            }
            r.Delete();
            try
            {
                DS.object_personAdapter.Update(DS.outpost.object_person);
                DS.alertAdapter.Update(DS.outpost.alert);
                DS.dvrAdapter.Update(DS.outpost.dvr);
                DS.objetcAdapter.Update(DS.outpost._object);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            if (objDataGridView.SelectedRows.Count == 1)
            {
                changeBtn.Enabled = true;
                deleteBtn.Enabled = true;
            }
            else
            {
                changeBtn.Enabled = false;
                deleteBtn.Enabled = false;
            }
        }


    }
}